package leetcode;


/**
 * 反转字符串中的单词 III
 */
public class LeetCode557 {
    public String reverseWords(String s) {
        char[] c = s.toCharArray();
        int i = 0;
        while (i < c.length) {
            int j = i;
            while (j < c.length && c[j] != ' ') j++;
            reverse(c, i, j - 1);
            i = j + 1;
        }
        return new String(c);
    }

    public void reverse(char[] c, int start, int end) {
        while (start < end) {
            char temp = c[start];
            c[start] = c[end];
            c[end] = temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        LeetCode557 leetCode557 = new LeetCode557();
        System.out.println(leetCode557.reverseWords("Let's take LeetCode contest"));
    }
}